import { AIPropertyDescriptor, IField } from "../AIProperty";
import { Field } from "./Field";

export class ButtonList extends Field implements IField {
    create(o: AIPropertyDescriptor, data: any) {
        let p = this._create(o, "ButtonList",data);
        let btns = (o as any).buttons as any[];
        let cont = p.getChild('cont');
        
        let path = 'editorResources/AIUI/propertyItem/Button.widget';
        
        btns.forEach( (button:any)=>{
            let bb = gui.UIPackage.createWidgetSync(path);
            let btn = bb.getChild('btn1') as gui.Button;
            let title = btn.getChild('title');
            btn.width=title.width+title.x*2;
            title.on('size_changed',(e:any)=>{
                //不会自动，只能手动设置button的大小
                btn.width=title.width+title.x*2;
            })
            //let bb = new gui.Button();
            btn.title = button.name;
            btn.on('click',(e:gui.Event)=>{button.onClick.call(btn,e)});
            cont.addChild(btn);
        })
        cont.on('size_changed',()=>{
            p.height = cont.height+10;
        })
        p.height = cont.height+10;

        this.data = data;
        return p;
    }
    // private _clickHandle(e: gui.Event) {
    //     console.log((e.target as any).name);
    //     console.log(this.data);
    // }
}